package com.example.common.commonsutils;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;
import lombok.Data;

import java.util.HashMap;

/**
 * @Author:tjf
 * @Description:
 */
@Schema
@Data
@Builder
public class R {
    @Schema(name = "请求是否成功标志")
    private boolean isSuccess;
    @Schema(name = "响应状态码")
    private int code;
    @Schema(name = "响应信息")
    private String message = "";
    @Schema(name = "响应数据")
    private HashMap<String, Object> data = new HashMap<>();
    private R(){}

    private R(boolean isSuccess, int code, String message, HashMap<String, Object> data) {
        this.isSuccess = isSuccess;
        this.code = code;
        this.message = message;
    }

    public static R ok(){
        return new RBuilder()
                .isSuccess(true)
                .code(ResponseCode.SUCCESS)
                .message("成功")
                .build();
    }
    public static R error(){
        return new RBuilder()
                .isSuccess(false)
                .code(ResponseCode.ERROR)
                .message("失败")
                .build();
    }
    public R isSuccess(boolean isSuccess){
        this.isSuccess = isSuccess;
        return this;
    }
    public R code(int code){
        this.code = code;
        return this;
    }
    public R message(String message){
        this.message = message;
        return this;
    }
    public R data(String key, Object value){
        this.data.put(key, value);
        return this;
    }
    public R data(HashMap<String, Object> data){
        this.data.putAll(data);
        return this;
    }
}
